﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ATMClientWinForm.ATMService;

namespace ATMClientWinForm
{
    public partial class ChangePIN : Form
    {
        Account acc;
        ATMService.ATMServiceSoapClient client;
        public ChangePIN(Account acc)
        {
            InitializeComponent();
            this.acc = acc;
            client = new ATMServiceSoapClient();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            ManageATM ad = new ManageATM(acc);
            ad.Show();
            this.Close();
        }

        void changePIN()
        {
            if (txtNewPIN.Text != "" || txtYourPIN.Text != "" || txtRePIN.Text != "")
            { 
                if( txtYourPIN.Text.Trim() == acc.Pin)
                {
                    if (txtNewPIN.Text.Trim() == txtRePIN.Text.Trim())
                    {
                        bool check = client.ChangePIN(txtNewPIN.Text.Trim(), acc.Ip);
                        if (check)
                        {
                            MessageBox.Show("Change PIN Success..!");
                            ManageATM ad = new ManageATM(acc);
                            ad.Show();
                            this.Close();
                        }
                        else
                            MessageBox.Show("Change PIN Failure..!");
                    }
                    else
                    {
                        txtNewPIN.Text = "";
                        txtRePIN.Text = "";
                        txtNewPIN.Focus();
                        MessageBox.Show("Re-Enter New PIN must the same New PIN !");
                    }
                }
                else
                {
                    txtYourPIN.Text = "";
                    txtNewPIN.Text = "";
                    txtRePIN.Text = "";
                    txtYourPIN.Focus();
                    MessageBox.Show("Incorrect PIN code");
                }
            }
            else
            {
                txtYourPIN.Text = "";
                txtNewPIN.Text = "";
                txtRePIN.Text = "";
                txtYourPIN.Focus();
                MessageBox.Show("Values is not null");
            }

        }
        private void txtYourPIN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {

                if (e.Handled = !char.IsNumber(e.KeyChar))
                {
                    MessageBox.Show("Must enter number");
                }

            }
        }

        private void txtNewPIN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {

                if (e.Handled = !char.IsNumber(e.KeyChar))
                {
                    MessageBox.Show("Must enter number");
                }

            }
        }

        private void txtRePIN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {

                if (e.Handled = !char.IsNumber(e.KeyChar))
                {
                    MessageBox.Show("Must enter number");
                }

            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            changePIN();
        }


    }
}
